/*
 * Created on 15.01.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.db.DBWrapper;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.struts.form.EditMessageForm;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.usr.messaging.Message;
import ro.gateway.aida.utils.PageToken;


/**
 * @author Mihai Postelnicu<p>
 *
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 */
public class MessageAction extends MultiTilesAction {
    private DBWrapper db;

    public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        if (user == null)
            return mapping.findForward("plslogin");

        db = DBWrapper.getManager(token);

        return modeSelect(mapping, form, request, response);
    }

    public ActionForward modeView(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
        long id = Long.parseLong(request.getParameter("id"));

        Message m = (Message) db.get(new Message(id, -1));

        if (m == null)
            return mapping.findForward("notfound");

        request.setAttribute("message", m);

        return mapping.findForward("view");
    }

    public ActionForward modeList(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();
        List sl = (List) session.getAttribute(LIST);

        if (sl == null)
            return modeRefresh(mapping, form, request, response);
        else {
        	PageToken.tokenizeList( request,  sc, sl, "messages");
            return mapping.findForward("list");
        }
    }

    public ActionForward modeRefresh(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
        HttpSession session = request.getSession();

        session.setAttribute(LIST, db.get(new Message(-1,user.getId())));
        List sl = (List) session.getAttribute(LIST);
        
        PageToken.tokenizeList( request,  sc, sl, "messages");
		
        return mapping.findForward("list");
    }

    public ActionForward modeEdit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);
		long id=Long.parseLong(request.getParameter("id"));
        EditMessageForm emf = (EditMessageForm) form;
       
        
        Message m=(Message)db.get(new Message(id,-1));
        emf.setId(m.getId());
        emf.setBody(m.getBody());
        emf.setSubject(m.getSubject());
        emf.setTxt(m.isTxt());

        return mapping.getInputForward();
    }

	public ActionForward modeSave(ActionMapping mapping, ActionForm form,
		HttpServletRequest request, HttpServletResponse response)
		throws Exception {
		User user = UserUtils.getLogged(request, response);

		EditMessageForm emf = (EditMessageForm) form;
		Message m = new Message(emf.getId(), user.getId());
		m.setSubject(emf.getSubject());
		m.setBody(emf.getBody());
		m.setTxt(emf.isTxt());
		db.synchronize(m);
		
		return modeRefresh(mapping,form,request,response);
	}


    public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        String mode = request.getParameter("mode");

        if ("view".equals(mode))
            return modeView(mapping, form, request, response);

        if ("refresh".equals(mode))
            return modeRefresh(mapping, form, request, response);

        if ("list".equals(mode))
            return modeList(mapping, form, request, response);

        if ("edit".equals(mode))
            return modeEdit(mapping, form, request, response);

		if ("save".equals(mode))
			return modeSave(mapping, form, request, response);

        return mapping.getInputForward();
    }

    public static final String LIST = "Message_LIST";
}
